const express=require('express');
const router= express.Router();

const User =require('../models/users');
// router.use(function(req, res, next) {
//     res.header("Access-Control-Allow-Origin", "*"); 
//     res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
//     next();
//   });
router.get('/mymulti',function(req,res){
    
    
    var reqid = req.query.用户.toString()
    const query = User.findOne({"name": reqid  }).sort({ field: 'asc', _id: -1 }); 
        query.setOptions({ lean : true });
        query.collection(User.collection);
        query.where('questions.编辑好的选择题').exists(). exec(function(err, userfound){
        if(err){
            console.log(err);
        }
        else{
            // if (userfound.questions.编辑好的选择题){}
            console.log(userfound.questions)
            res.send(JSON.stringify(userfound.questions))
        }
    }
    )
})
router.post('/mymulti/upload',function(req,res){
    try{
    res.send('服务器已收到报文：：'+JSON.stringify(req.body))
    console.log('收到选择题')
    var user =new User(req.body)
    user.save()
}catch(error){console.log(error)}
}
)



router.get('/multiquesfrom/:id',function(req,res){
    var reqid = req.params.id.toString()
    const query = User.findOne({"name": reqid  }).sort({ field: 'asc', _id: -1 }); 
        query.setOptions({ lean : true });
        query.collection(User.collection);
        query.where('questions.编辑好的选择题').exists(). exec(function(err, userfound){
        if(userfound){
            // if (userfound.questions.编辑好的选择题){}
            try{console.log(userfound.questions)
            res.send(JSON.stringify(userfound.questions))}catch{(console.error())}
        }else{res.send('该用户没有问卷，或用户不存在')}
    }
    )
    
})




module.exports=router